<template>
	<div class="orderDetail">
		<!-- 头部筛选区域 -->
		<ChoiseHeader 
			:choiseList='choiseList'
			:isDali='isDali' 
			@chageData='chageData' >
		</ChoiseHeader>
		<!-- 订单显示区域 -->
		<OrderDataList 
			:orderList='orderList'
			:more="more"
			:LoadIng='LoadIng'
			:noData='noData'
			@getNextPage='getNextPage'>
		</OrderDataList>
		
		<TotalBar :orderTotal='orderTotal' :priceTotal='priceTotal'></TotalBar>
	</div>

	

</template>

<script type="text/javascript">
	import ChoiseHeader from '@/components/ChoiseHeader'
	import OrderDataList from '@/components/OrderDataList'
	import TotalBar from '@/components/TotalBar'
	import {get} from '../../utils/index'
	export default{
		
		data(){
			return {
				LoadIng:true,
				more:true,
				days : '',
				isDali:false, //是否是代理商
				user:{},
				AgentList:[], //代理商下面的加盟商、合作商、承包商数据列表 
				dateList:[],
				typeList:[],
				coffeeMaker:[], //咖啡机筛选列表
				curAgent:{},
				curDate:{},
				curType:{},
				curCoffeeMaker:{},
				page:0,
				orderList:[],
				leagueId:-2,
				noData:false,

				orderTotal:0,
				priceTotal:0
				
			}
		},
		mounted(){
			this.days = this.$root.$mp.query.days
			this.user = wx.getStorageSync('userDo');
			if(this.user.type == 1){
				this.isDali = true
				this.leagueId = -1
			}
			this.initData();
			this.getOrderList(true);
			this.getTotal();
			
		},
		computed:{
			choiseList(){
				let list = []
				list = [ this.AgentList,this.typeList,this.dateList,this.coffeeMaker]
				return  list
			}
		},
		methods:{
			async initData(){
				//语言选择的种类 0:简体 1:繁体
				var font = this.user.font
				//获取时间筛选数据
				this.dateList = await get('dictList/time_interval/'+font)

				//获取 代理商下面的加盟商、合作商、承包商数据列表 
				var agentData = {
					select: 0,
			        limit: 9999,
			        offset: 0,
			        parent: this.user.id,
			        type: 2
				}
				this.AgentList = await get('getAgentListByAgentId',agentData)

				//获取咖啡机筛选列表
				var user = this.AgentList[0]
				await this.getCoffeeMakerList(user);
				this.typeList = [
		          {id:'',name:'全部'},
		          {id:1,name:'咖啡'},
		          {id:2,name:'商品'}
		        ]
		       
			},
			

			async getCoffeeMakerList(user){
				var coffeeData = {
					type: user.type < 0 ? wx.getStorageSync('userDo').type : user.type,
		            userId: user.type < 0 ? wx.getStorageSync('userDo').userId : user.userId,
		            leagueNull: user.userId != -1 ? 0 : (wx.getStorageSync('userDo').type == 1 ? -1 : 0)
				}
				this.coffeeMaker = await get('getMakerListByUserIdNoPage',coffeeData)
			},

			async getTotal(){
				var user = wx.getStorageSync('userDo');
				var data = {
					makerUserId : user.userId,
		            userTypee   : user.type,
		            saleType    : this.curType.id || "",
		            time        : this.curDate.value || 0,
		            makerId     : this.curCoffeeMaker.id || '',
		            date        : this.days,
		            leagueId    : this.curAgent.userId || this.leagueId
				}
				get('coffeeOrderCount',data).then( res =>{
					console.log(res)
					this.orderTotal = res.orderCount;
					this.priceTotal = res.orderPay
				})
				.catch(err =>{
					console('err'+err)
				})

			},

			getOrderList(init){
				if(init){
					this.page = 0
			        this.more = true
				}
				this.LoadIng = true
				var user = wx.getStorageSync('userDo');
				var userType = wx.getStorageSync('userDo').type;
				var data = {
					day           :   this.curDate.value || 0 ,
		            type          :   user.type,
		            userId        :   user.userId,
		            offset        :   this.page,
		            id            :   this.curCoffeeMaker.id || '',
		            saleType      :   this.curType.id ||'' ,
		            date          :   this.days,
		            leagueId      :   userType == 3 ? -2:(this.curAgent.userId || this.leagueId),
		            font          :   user.font
				}
				get('getOrderMsgByUserId',data).then( res =>{
					var list = res
					if( list.length == 0 &&　this.page == 0 ){
						this.noData = true
					}else{
						this.noData = false
					}

					if(list.length < 10){
						this.LoadIng = false;

						if(this.page > 0){
							this.more = false
						}
					}

					if(init){
						this.orderList = list
					}else{
       					this.orderList = this.orderList.concat(list)
					}	
				})
				.catch(err =>{
					console('err'+err)
				})
			},
			chageData(item,idx){
				if(idx == 0){
					this.curAgent = item  

					this.getCoffeeMakerList(item);
				}else if(idx == 1){
					this.curType = item 
				}else if( idx == 2){
					this.curDate =item
				}else{                    
					this.curCoffeeMaker = item 
					
				}
				this.getOrderList(true);
				this.getTotal();
			},

			// 滚动翻页
			getNextPage(){
				if(!this.more){
					return
				}
				this.page = this.page+1
				this.getOrderList();
			}
		},
		onUnload() {
	        Object.assign(this.$data, this.$options.data())
	    },
		
		components:{
			ChoiseHeader,
			OrderDataList,
			TotalBar,
		}
	}
</script>
<style>
	page{background:#fafafa; position:relative;}
	.orderDetail{
		position: fixed;
		z-index:10;
		top:0;
		left:0;
		right:0;
		bottom:0;
		background:#fff;
	}
</style>